/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.srdxeditor.dialogs;


import net.miblounge.gradecalculator.core.descriptors.StudyRegulationsDescription;
import net.miblounge.gradecalculator.guicommon.dialogs.AbstractDialog;
import net.miblounge.gradecalculator.guicommon.layouts.VerySimpleLayout;
import net.miblounge.gradecalculator.guicommon.util.StringUtil;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QWidget;

public class StudyRegulationsDescriptionDialog extends AbstractDialog {
	
	private QLineEdit leName;
	private QLineEdit leSemester;
	private QComboBox cbUniversity;
	private QComboBox cbDegree;
	
	private final StudyRegulationsDescription reg;
	
	public StudyRegulationsDescriptionDialog(final QWidget parent, final StudyRegulationsDescription reg)
	{
		super(parent);
		setWindowTitle(tr("Regulations"));
		
		this.reg = reg;

		setClientArea(createClientArea());

		leName.setFocus();
		leName.setSelection(0, leName.text().length());
		
		revalidate();
	}
	
	private QWidget createClientArea()
	{
		final QGroupBox group = new QGroupBox(tr("Regulations"), this);
		final VerySimpleLayout layoutGroup = new VerySimpleLayout(group);
		group.setLayout(layoutGroup);

		final QWidget container = new QWidget(group);
		layoutGroup.setWidget(container);
		final MigLayout containerLayout = new MigLayout("", "[][grow,fill]15[grow]"); 
		container.setLayout(containerLayout);
		
		containerLayout.addWidget(new QLabel(tr("Name:")));
		leName = new QLineEdit(reg.getModuleList().getName(), container);
		leName.textEdited.connect(this, "revalidate()");
		containerLayout.addWidget(leName, "span, growx, w 100:200:");

		containerLayout.addWidget(new QLabel(tr("University:")));
		cbUniversity = new QComboBox(container);
		cbUniversity.setEditable(true);
		cbUniversity.setEditText(reg.getUniversity());
		cbUniversity.editTextChanged.connect(this, "revalidate()");
		containerLayout.addWidget(cbUniversity, "span, growx, w 100:200:");

		containerLayout.addWidget(new QLabel(tr("Degree:")));
		cbDegree = new QComboBox(container);
		cbDegree.setEditable(true);
		cbDegree.setEditText(reg.getDegree());
		cbDegree.editTextChanged.connect(this, "revalidate()");
		containerLayout.addWidget(cbDegree, "growx, w 100:200:200");
		
		containerLayout.addWidget(new QLabel(tr("Semester:")), "split");
		leSemester = new QLineEdit(reg.getSemester(), container);
		leSemester.textEdited.connect(this, "revalidate()");
		containerLayout.addWidget(leSemester, "span, growx, w 100!");

		return group;
	}
	
	protected boolean validate()
	{
		if (StringUtil.cleanString(leName.text()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(leSemester.text()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(cbUniversity.currentText()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(cbDegree.currentText()).equals("")) {
			return false;
		}
		
		return super.validate();
	}

	
	public void apply()
	{
		super.apply();
		reg.getModuleList().setName(StringUtil.cleanString(leName.text()));
		reg.setUniversity(StringUtil.cleanString(cbUniversity.currentText()));
		reg.setDegree(StringUtil.cleanString(cbDegree.currentText()));
		reg.setSemester(StringUtil.cleanString(leSemester.text()));
	}

}
